package tankGame02;

import tankGame02.tank.Enemy;
import tankGame02.tank.Hero;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-01
 * Time:11:58
 */
@SuppressWarnings("all")
public class MyPanel extends JPanel implements KeyListener {
    Hero hero = null;
    Vector<Enemy> enemies = new Vector<>();
    int enemySize = 3;

    public MyPanel() {
        hero = new Hero(100, 100);
        for (int i = 0; i < enemySize; i++) {
            enemies.add(new Enemy(100 * (i + 1), 0, 2));
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1000, 750);
        drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), hero.getSize(), 0);
        for (Enemy enemy : enemies) {
            drawTank(enemy.getX(), enemy.getY(), g, enemy.getDirect(), enemy.getSize(), 1);
        }
    }

    public void drawTank(int x, int y, Graphics g, int direct, int size, int role) {
        if (role == 0) {
            drawHero(x, y, g, direct, size);
        } else {
            drawEnemy(x, y, g, direct, size);
        }
    }

    private void drawHero(int x, int y, Graphics g, int direct, int size) {
        Image image = switch (direct) {
            case 0 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/hero2/heroUp.png"));
            case 1 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/hero2/heroRight.png"));
            case 2 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/hero2/heroDown.png"));
            case 3 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/hero2/heroLeft.png"));
            default -> null;
        };
        g.drawImage(image, x, y, size, size, this);
    }

    private void drawEnemy(int x, int y, Graphics g, int direct, int size) {
        Image image = switch (direct) {
            case 0 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/enemy2/enemyUp.png"));
            case 1 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/enemy2/enemyRight.png"));
            case 2 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/enemy2/enemyDown.png"));
            case 3 -> Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/tankGame02/enemy2/enemyLeft.png"));
            default -> null;
        };
        g.drawImage(image, x, y, size, size, this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
            hero.move(0, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
            hero.move(2, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
            hero.move(1, hero.getSpeed());
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
            hero.move(3, hero.getSpeed());
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}
